package com.android.customviewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * 自定义ViewGroup
 */
public class CustomViewGroup extends ViewGroup {

    private int mColumnNumbers = 2;

    private int mRowNumbers = 0;


    public CustomViewGroup(Context context) {
        this(context, null);
    }

    public CustomViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /***
     * 获取子View中宽度最大的值
     */
    private int getMaxChildWidth() {
        int childCount = getChildCount();
        int maxWidth = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            if (childView.getMeasuredWidth() > maxWidth)
                maxWidth = childView.getMeasuredWidth();

        }

        return maxWidth;
    }

    /***
     * 将所有子View的高度相加
     **/
    private int getTotleHeight() {
        View childView = getChildAt(0);
        return childView.getMeasuredHeight() * mRowNumbers;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //将所有的子View进行测量，这会触发每个子View的onMeasure函数
        //注意要与measureChild区分，measureChild是对单个view进行测量
        //measureChildren(widthMeasureSpec, heightMeasureSpec);

        mRowNumbers = getChildCount() % mColumnNumbers == 0 ? getChildCount() / mColumnNumbers :
                getChildCount() / mColumnNumbers + 1;
        //子view宽高
        int childHeightMeasureSpec = 0;//高度测量模式
        int childWidthMeasureSpec = 0;//宽度测量模式
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
            childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth() / mColumnNumbers
                    , MeasureSpec.EXACTLY);
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childView.getMeasuredHeight(),
                    MeasureSpec.AT_MOST);
            childView.measure(childWidthMeasureSpec, childWidthMeasureSpec);
        }

        //view宽高
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int childCount = getChildCount();
        if (childCount == 0) {//如果没有子View,当前ViewGroup没有存在的意义，不用占用空间
            setMeasuredDimension(0, 0);
        } else {
            //MeasureSpec.AT_MOST wrap_content自适应模式
            //MeasureSpec.EXACTLY match_parent或者具体尺寸模式
            if (widthMode == MeasureSpec.AT_MOST && widthMode == MeasureSpec.AT_MOST) {
                int width = getMaxChildWidth();
                int height = getTotleHeight();
                setMeasuredDimension(width, height);
            } else if (widthMode == MeasureSpec.AT_MOST) {
                setMeasuredDimension(getMaxChildWidth(), heightSize);
            } else if (heightMode == MeasureSpec.AT_MOST) {
                setMeasuredDimension(widthSize, getTotleHeight());
            } else {
                setMeasuredDimension(widthSize, getTotleHeight());
            }
        }
    }


    /**
     * 为所有的子控件摆放位置.
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int currentHeight = 0;
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            int height = view.getMeasuredHeight();
            int width = view.getMeasuredWidth();
            if (i % 2 == 0) {
                view.layout(l, currentHeight, l + width, currentHeight + height);
            } else {
                view.layout(l + width, currentHeight, l + width + width,
                        currentHeight + height);
                currentHeight += height;
            }
        }
    }

    public void setViewGroupAdapter(ViewGroupAdapter<TestBean> viewGroupAdapter) {
        removeAllViews();
        for (int i = 0; i < viewGroupAdapter.getCount(); i++) {
            View view = viewGroupAdapter.getView(null, i);
            this.addView(view);
        }
    }
}

